﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


using miniCCR;

namespace SimpleGameAppCCR.NComp
{


    class Sprite_Proxy : miniCCR.Proxy
    {

        public Sprite_Proxy(GComponent prom, GComponent to)
            : base(prom,to)
        { }
    

        internal iTask GetPosition(SimpleGameAppCCR.Operation.GetPosition pos)
        {
            //iTask task = CreateTask(pos, pos);
            
            return Post(pos, pos);
        }

        internal iTask SetPosition(SimpleGameAppCCR.Operation.SetPosition pos)
        {
            

            return Post(pos, null);
           
        }
    }




    class Sprite : miniCCR.GComponent
    {


        class OP_Draw : Update
        {

        }


        public override uint _ComID
        {
            get { return (uint)COMID.SPRITE; }
        }


        public override string GetProxyName()
        {
            return typeof(Sprite_Proxy).Name;
        }

        public override miniCCR.Proxy GetProxy(GComponent from)
        {
            Sprite_Proxy proxy = new Sprite_Proxy(from,this);


            return proxy;
        }

        public static List<Sprite> _RenderList = new List<Sprite>();

        public Vector2 _mPosition = new Vector2(0, 0);

        Texture2D _mSpriteTexture;

        //public override String GetCompType()
        //{
        //    return typeof(Sprite).Name;
        //}

        public Sprite(Texture2D mSpriteTexture)
        {
            _mSpriteTexture = mSpriteTexture ;
        }

        public IEnumerator<miniCCR.iTask> DrawHandle(Operation.Draw op)
        {

            if (_mPosition.X < 600 && _mPosition.Y < 600)
            {
                op.spriteBatch.Draw(_mSpriteTexture, _mPosition, Color.White); 
            }

            yield break;

        }

        public IEnumerator<miniCCR.iTask> SetPositionHandle(Operation.SetPosition op)
        {

            _mPosition = op._Pos;

            yield break;

        }


        public IEnumerator<miniCCR.iTask> GetPositionHandle(Operation.GetPosition op)
        {
            op._Pos = _mPosition;

            yield return op.FromProxy.Post(op);

            yield break;

        }



        public override IEnumerator<miniCCR.iTask> OnMessage(miniCCR.Operation op)
        {
            if (op.ToString() == typeof(Operation.Draw).ToString())
            {
                return DrawHandle(op as Operation.Draw);
            }
            else if (op.ToString() == typeof(Operation.SetPosition).ToString())
            {
                return SetPositionHandle(op as Operation.SetPosition);
            }
            else if (op.ToString() == typeof(Operation.GetPosition).ToString())
            {
                return GetPositionHandle(op as Operation.GetPosition);
            }

            return null;
        }

    }



}
